//
//  DeliveriesCell.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/16.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "DeliveriesCell.h"

@interface DeliveriesCell()<UIAlertViewDelegate>{
    UIView *_orderDetailView;
    UIView *_remarkView;
    
    NSUInteger _detailViewHeight;
    
    UILabel *_staffLabel;
    UILabel *_requireTimeLabel;
    
    UIButton *_call;
    UILabel *_userNameLabel;
    UILabel *_addressLabel;
    UILabel *_paymentLabel;
    UILabel *_moneyLabel;
    
    UIView *_spacingView;
    
    NSString *mobile;
    
    UIView *_line1View;
    UIView *_line2View;
    UIView *_line3View;
}

@end

@implementation DeliveriesCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier frame:(CGRect)frame{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier frame:frame];
    if (self) {
        [self initSubViewWithFrame:frame];
    }
    return self;
}

-(void)initSubViewWithFrame: (CGRect) frame{
    
    _spacingView = [[UIView alloc] init];
    [_spacingView setBackgroundColor:PanelBgColorDefault];
    [self addSubview:_spacingView];
    
    _orderDetailView = [[UIView alloc] init];
    [self addSubview:_orderDetailView];
    _remarkView = [[UIView alloc] init];
    [_remarkView setBackgroundColor:PanelBgColorRemark];
    [self addSubview:_remarkView];
    
    // 配送员
    _staffLabel = [[UILabel alloc] initWithFrame: CGRectMake(kOrderCellMargin, 0, frame.size.width * 0.68 - kOrderCellMargin, kOrderCellHeaderHeight)];
    _staffLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _staffLabel.textColor = TextColorImportant;
    [self addSubview:_staffLabel];
    
//    [self.drawLineHelper addHorizontalLine:0 y:kOrderCellHeaderHeight color:LineColorImportant width:frame.size.width * 0.68];
    _line1View = [[UIView alloc] initWithFrame: CGRectMake(0, kOrderCellHeaderHeight, frame.size.width * 0.68, 1)];
    _line1View.backgroundColor = LineColorImportant;
    [self addSubview: _line1View];
    
    // 用户要求
    _requireTimeLabel = [[UILabel alloc] initWithFrame: CGRectMake(kOrderCellMargin, kOrderCellHeaderHeight, frame.size.width - kOrderCellSpacing * 2, kOrderCellHeaderHeight)];
    _requireTimeLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _requireTimeLabel.textColor = TextColorImportant;
    [self addSubview:_requireTimeLabel];
    
//    [self.drawLineHelper addHorizontalLine:kOrderCellMargin y:kOrderCellHeaderHeight*2 color:LineColorDefault width:frame.size.width - kOrderCellMargin * 2];
    
    _line2View = [[UIView alloc] initWithFrame: CGRectMake(kOrderCellMargin, kOrderCellHeaderHeight*2, frame.size.width - kOrderCellMargin * 2, 1)];
    _line2View.backgroundColor = LineColorDefault ;
    [self addSubview: _line2View];

    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _moneyLabel.textColor = TextColorImportant;
    [self addSubview: _moneyLabel];
    
    _userNameLabel = [[UILabel alloc] init];
    _userNameLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _userNameLabel.textColor = TextColorDefault;
    [self addSubview: _userNameLabel];
    
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _addressLabel.textColor = TextColorDefault;
    _addressLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _addressLabel.numberOfLines = 0;
    [self addSubview: _addressLabel];
    
    _paymentLabel = [[UILabel alloc] init];
    _paymentLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _paymentLabel.textColor = TextColorLight;
    [self addSubview: _paymentLabel];

    _call = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_call setTitle:@"拨打电话" forState:UIControlStateNormal];
    [_call setTitleColor:TextColorSpecialButton forState:UIControlStateNormal];
    [_call setBackgroundColor:[UIColor clearColor]];
    [_call.layer setMasksToBounds:YES];
    [_call.layer setCornerRadius:16.0]; //设置矩形四个圆角半径
    [_call.layer setBorderWidth:1.0];   //边框宽度
    [_call.layer setBorderColor: TextColorSpecialButton.CGColor];
    [self addSubview:_call];
    CGRect callRect = CGRectMake(self.bounds.size.width - kOrderCellSpacing - 88, kOrderCellSpacing, 88, 36);
    _call.frame = callRect;

    _line3View = [[UIView alloc] init];
    _line3View.backgroundColor = LineColorDefault;
    [self addSubview: _line3View];
    
    [_call addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark -点击拨打电话先获得允许
 -(void)butClick:(id)inSender {
     
     [JFBUIAnimation alertSure:@"确认拨打电话？" delegate:self];
//
//     NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", mobile];
//     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[NSMutableString alloc] initWithFormat:@"tel:%@", mobile]]];
//     NSMutableString * telstr=[[NSMutableString alloc] initWithFormat:@"tel:%@", mobile];
//     UIWebView * callWebview = [[UIWebView alloc] init];
//     [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:telstr]]];
//     [self addSubview:callWebview];
 }
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"phone call %@", mobile);
    if( buttonIndex == 1 ) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", mobile];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}

-(void)setOrder:(JFBOrderDS *)order{
    
    NSString *staff = [NSString stringWithFormat:@"配送员：%@(%@)", order.staff, order.staffMobile];
    _staffLabel.text = staff;
  
    mobile = order.staffMobile;

    _requireTimeLabel.text = order.remark;
    
    NSString *money = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.money]];
    CGSize moneySize = [money sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat moneyY = kOrderCellHeaderHeight * 2;
    CGFloat moneyX = self.bounds.size.width - moneySize.width - kOrderCellMargin;
    CGRect moneyRect = CGRectMake(moneyX, moneyY, moneySize.width, kOrderCellHeaderHeight);
    _moneyLabel.frame = moneyRect;
    _moneyLabel.text = money;

    NSString * userName = [NSString stringWithFormat:@"%@（%@）", order.userName, order.userMobile];
    CGSize userNameSize = [userName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat userNameY = kOrderCellHeaderHeight * 2;
    CGFloat userNameX = kOrderCellMargin;
    CGRect userNameRect = CGRectMake(userNameX, userNameY, moneyX - kOrderCellMargin, kOrderCellHeaderHeight);
        _userNameLabel.frame = userNameRect;
    _userNameLabel.text = userName;

    
    CGSize paymentSize = [order.payment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat paymentY = CGRectGetMaxY(_userNameLabel.frame) + kOrderCellSpacing;
    CGFloat paymentX = self.bounds.size.width - paymentSize.width - kOrderCellMargin;
    CGRect paymentRect = CGRectMake(paymentX, paymentY, paymentSize.width, paymentSize.height);
    _paymentLabel.text = order.payment;
    _paymentLabel.frame = paymentRect;
    
    CGFloat addressY = CGRectGetMaxY(_userNameLabel.frame) + kOrderCellSpacing;
    CGFloat addressX = kOrderCellMargin;
    _addressLabel.text = order.address;
    CGSize addressSize = [_addressLabel sizeThatFits:CGSizeMake(paymentX - kOrderCellMargin, MAXFLOAT)];
    CGRect addressRect = CGRectMake(addressX, addressY, paymentX - kOrderCellMargin, addressSize.height);
    _addressLabel.frame = addressRect;

    
    if( [order.merchandiseList count] != 0 ) {
        for( UIView *v in _orderDetailView.subviews ) {
            [v removeFromSuperview];
        }
        
        CGRect imageContainerRect = CGRectMake(0, CGRectGetMaxY(_addressLabel.frame) + kOrderCellSpacing, self.bounds.size.width, kOrderCellImageSize + kOrderCellSpacing * 2);
        _orderDetailView.frame = imageContainerRect;
        
        NSUInteger length = order.merchandiseList.count > 4 ? 4 : order.merchandiseList.count;
        for( int i = 0; i < length; i++ ){
            CGRect frame = CGRectMake(kOrderCellSpacing * (i + 1) + kOrderCellImageSize * i, kOrderCellSpacing, kOrderCellImageSize, kOrderCellImageSize);
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
            
            if (i == 3)
            {
                [imageView setImage:[UIImage imageNamed:@"icon-OrderItem-More"]];
                
            }else{
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://wup.jufangbian.com/upload/product_image/product_150/%lu.jpg", (unsigned long)((JFBMerchandiseDS*)[order.merchandiseList objectAtIndex:i]).mpId]] placeholderImage:[UIImage imageNamed:ImageDefaultPlaceholder]];
                [imageView.layer setBorderWidth:1];
                UIColor *defaultColor = LineColorDefault;
                [imageView.layer setBorderColor:defaultColor.CGColor];
                
            }
            [_orderDetailView addSubview:imageView];
            
        }
        UILabel *totalCountLabel = [[UILabel alloc] init];
        NSString *totalCount = [NSString stringWithFormat:@"共%lu件", (unsigned long)order.totalCount];
        CGSize totalCountSize = [totalCount sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
        CGFloat totalCountX = _orderDetailView.bounds.size.width - kOrderCellSpacing - totalCountSize.width, totalCountY = (_orderDetailView.bounds.size.height - totalCountSize.height) / 2;
        CGRect totalCountRect = CGRectMake(totalCountX, totalCountY, totalCountSize.width, totalCountSize.height);
        totalCountLabel.textColor = TextColorDefault;
        totalCountLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
        totalCountLabel.text = totalCount;
        totalCountLabel.frame = totalCountRect;
        [_orderDetailView addSubview:totalCountLabel];
        
    } else {
        CGRect imageContainerRect = CGRectMake(0, addressY + addressSize.height + kOrderCellSpacing, self.bounds.size.width, 0);
        _orderDetailView.frame = imageContainerRect;
    }

    _line3View.frame = CGRectMake(kOrderCellMargin, _orderDetailView.frame.origin.y, self.bounds.size.width - kOrderCellMargin * 2, 1);

    
    _spacingView.frame = CGRectMake(0, CGRectGetMaxY(_orderDetailView.frame), self.bounds.size.width, kOrderCellSpacing);
    
    //设置Cell高度
    self.height = CGRectGetMaxY(_spacingView.frame);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
